package com.vf.admin.client.controller.trip;

import cn.hutool.core.util.ObjectUtil;
import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.service.AppSetupService;
import com.vf.admin.client.service.AppTripFriendService;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppSetup;
import com.vf.admin.miaodong.entity.AppTripFriend;
import com.vf.admin.miaodong.entity.AppUser;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * <p>Title : 旅游</p>
 * <p>Description : </p>
 * <p>DevelopTools : IDEA</p>
 * <p>DevelopSystem : Windows11</p>
 *
 * @Author : zxw
 * @Date 2023/9/4 16:11
 */
@RestController
@RequestMapping("/app/trip")
public class TripController extends ClientBaseController {


    @Resource
    private AppTripFriendService appTripFriendService;
    @Resource
    private AppUserService appUserService;
    @Resource
    private AppSetupService appSetupService;



    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
    public Result save(AppTripFriend param) {

        try {
            AppUser appUser = appUserService.get(getUserId());
            //用户id
            param.setUserId(appUser.getId());
            //性别
            param.setSex(appUser.getSex());
            appTripFriendService.save(param);
            return this.sucessJSON("保存成功");
        } catch (Exception e) {
            return this.errorJSON("系统繁忙");
        }
    }


    @ResponseBody
    @RequestMapping("/list")
    public Result list(Integer pageno) {

        SplitPageBean splitPageBean = new SplitPageBean(pageno, 20);
        //获取当前登录用户的user对象
        AppUser user = appUserService.get(getUserId());
        HashMap<String, Object> params = new HashMap<>();

        //男号返回女号  女号返回男号   未知返回女号
        if (ObjectUtil.isNotEmpty(user)) {
            if (user.getSex().equals(1)) {
                params.put("sex", 2);
                params.put("isReal", 0);
            }
            if (user.getSex().equals(2)) {
                params.put("sex", 1);
                params.put("isReal", 1);
            }
            if (user.getSex().equals(3)) {
                params.put("sex", 2);
                params.put("isReal", 0);
            }
        }
        AppSetup appSetup = appSetupService.get();
        if (ObjectUtil.isNotEmpty(appSetup)){

            if (appSetup.getAuditStatus().equals(0)){
                String ids = appSetup.getTripIds();
                params.put("ids",ids);

            }
        }

        List<AppTripFriend> list = appTripFriendService.list(params,splitPageBean);
        return this.sucessPage(list, splitPageBean.getNextPageNo());
    }


}

